<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" %>
<%@ page import="org.springframework.web.servlet.mvc.method.RequestMappingInfo" %>
<%@ page import="org.springframework.web.method.HandlerMethod" %>
<%@ page import="java.util.Map" %>
<%@ page import="org.springframework.web.servlet.mvc.condition.PatternsRequestCondition" %>
<%@ page import="java.util.Set" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取请求路径对应的JAVA类和具体的方法</title>
</head>
<body>
<%
    //获取WebApplicationContext，用于获取Bean
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(application);
    //获取spring容器中的RequestMappingHandlerMapping
    RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) webApplicationContext.getBean("requestMappingHandlerMapping");
    //获取应用中所有的请求
    Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
    int requestCount = 0;
    int handlerCount = handlerMethods.size();
    //遍历所有的请求信息
    for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
        //key为http请求信息，即URL
        RequestMappingInfo requestMappingInfo = entry.getKey();
        //value为处理请求信息的方法，即code
        HandlerMethod handlerMethod = entry.getValue();
        //URL
        PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
        Set<String> patterns = patternsCondition.getPatterns();
        requestCount += patterns.size();
        //类和方法
        String type = handlerMethod.getBeanType().getName();
        String method = handlerMethod.getMethod().getName();
        out.println("<span>URL:</span>" + patterns);
        out.println("<br/>");
        out.println("<span>类：</span>" + type);
        out.println("<br/>");
        out.println("<span>方法：</span>" + method);
        out.println("<hr/>");
    }
    out.println("<h5>请求总数：" + requestCount + "       处理器总数：" + handlerCount + "</h5>");
%>
</body>
</html>

